﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

using MAPDavy.Entities;
using MAPDavy.BusinessLogic;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace MAPDavy.WebMVC3.Controllers
{
    public class ReportsController : Controller
    {
        public ReportsController()
        {
            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                container.BuildUp(this.GetType(), this);
            }
        }

        //
        // GET: /Reports/
        [Dependency]
        public IReportBL ReportBL { get; set; }

        public ActionResult Index()
        {
            var _reports = ReportBL.GetAllFromReport();

            return View(_reports);
        }

        [Authorize(Roles = "admin,director")]
        public ActionResult VerReporte(string id)
        {
            var reporte = ReportBL.GetFromReportById(Int32.Parse(id));

            if (reporte != null)
            {
                ViewData["report"] = id;
            }
            else
            {
                return RedirectToAction("Index", "Reports");
            }           


            return View();
        }

        [Authorize(Roles = "admin,director")]
        public JsonResult Viewer(string id)
        {

            string _reporte = "report=" + id;
             
            StringBuilder sb = new StringBuilder();
            sb.Append("<iframe id='ifReporte' width='100%' style='height:100%;' frameborder='0'");
            //sb.AppendFormat("src='/ReportViewer.aspx?" + _reporte + "'");
            sb.AppendFormat("src='" + Url.Content("~/ReportViewer.aspx?" + _reporte) + "'");
            sb.Append("></iframe>");

            return this.Json(sb.ToString(), JsonRequestBehavior.AllowGet);
        }
    }
}
